
//根据cookie信息显示导航条
function showHeadBox()
{
	var username=$.cookie('lvmao_username')?$.cookie('lvmao_username'):'';
	if(username=='')
	{
		$('#head_box1').show();
		$('#head_box2').hide();
	}
	else
	{
		$('#head_box2').html($('#head_box2').html().replace('$username$',username));
		$('#head_box2').show();
		$('#head_box1').hide();
	}
}

//根据cookie信息显示登录框
function GetUser()
{
	var username=$.cookie('lvmao_username')?$.cookie('lvmao_username'):'';
	if(username=='')
	{
		$('#nologin').show();
	}
	else
	{
		ShowIslogin();
	}
}

function ShowIslogin()
{
	$('#islogin').html($('#islogin').html().replace('$username$',$.cookie('lvmao_username')));
	$('#islogin').show();
}

//ajax登录
function GoLogin()
{
	var username=$('#user_name').val();
	var username2='';
	//username=encodeURI(username);
	var password=$('#pass_word').val();
	
	if(username=='' || password=='')
	{
		alert('用户名和密码不能为空');
	}
	else
	{
		$('#nologin').hide();
		$('#loging').show();

		//登录主站
		$.post("/user/login.aspx",{username:username,password:password},function(data){
			datas=data.split('-');
			//alert(data);
			if(datas[0]=='1')
			{
				//setTimeout(function(){LoginCms(datas[1],password);},2000);
				setTimeout(function(){LoginCms(username,password);},1000);
			}
			else
			{
				alert('登录失败');
				$('#pass_word').val('');
				$('#loging').hide();
				$('#nologin').show();
			}
		});
		
		//LoginCms(username,password);
		
		
	}
}
		
function LoginCms(username,password)
{

	$.post("/info/member/login.php",{action:"ajax",username:username,password:password},function(data){
		//alert(data);
		if(data=='1' || data.indexOf('已经登'))
		{
			setTimeout(function(){LoginWiki(username,password);},1000);
		}
	});
}

function LoginWiki(username,password)
{

	$.post("/wiki/index.php?user-login",{username:username,password:password,submit:"%E7%99%BB%E5%BD%95"},function(data){
	LoginRedirect();
		//alert(data);
//		if(data=='1' || data.indexOf('已经登'))
//		{
//			
//		}
	});
}

function LoginRedirect()
{
	//alert(document.getElementById('fromurl'));
	if(document.getElementById('fromurl'))
	{
		//alert(document.getElementById('fromurl').value);
		if(document.getElementById('fromurl').value!="")
		{
			location.href=$('#fromurl').val();
		}
		else
		{
			location.href='/';	
		}
	}
	else
	{
		history.go(0);	
	}
	
	
}



function checkreguser(username)
{
	if(username=="")
	{
		showflag("usernameBox","0","用户名不能为空");
		return 0;
	}
	
	var re;
	//$.get("/user/regcheck.aspx?username="+username,null,function(data){
	jQuery.ajax({ type: "GET", url: "/user/regcheck.aspx?username="+username+"&x="+Math.random(), async:false, success: function(data){
		if(data=="1")
		{
			showflag("usernameBox","1","用户名可用");
			re=1;
		}
		else
		{
			showflag("usernameBox","0","用户名已被占用");
			re=0;
		}
	}});
	
	return re;

}


function checkregemail(email)
{
	if(email=="")
	{
		showflag("emailBox","0","email不能为空");
		return false;
	}
	
	if(ismail(email)==false)
	{
		showflag("emailBox","0","email格式不正确");
		return false;
	}
	
	var re;
	
	jQuery.ajax({ type: "GET", url: "/user/regcheck.aspx?email="+email+"&x="+Math.random(), async:false, success: function(data)
	{
		if(data=="1")
		{
			showflag("emailBox","1","email可用");
			re=true;
		}
		else
		{
			showflag("emailBox","0","email已被占用");
			re=false;
		}
	}
	});
	
	return re;
}


function checkregpwd()
{
	var pwd1=$('#password').val();
	var pwd2=$('#password2').val();
	
	if(pwd1.length<6)
	{
		showflag("pwd1Box","0","密码不能小于6位");
		return false;
	}
	else
	{
		showflag("pwd1Box","1","");
	}
	
	if(pwd2.length<6)
	{
		showflag("pwd2Box","0","密码不能小于6位");
		return false;
	}
	else
	{
		showflag("pwd2Box","1","");
	}
	
	if(pwd1!=pwd2)
	{
		showflag("pwd2Box","0","两次输入的密码不一致");
		return false;
	}
	
	return true;
}

function showflag(id,flag,info)
{
	$('#'+id).html('<img src="/img/regcheck'+flag+'.jpg" width=16 />'+info);
}

function ismail(email)
{
	return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(email));
}



//用JS给表单的元素赋值，不支持Name相同但Type不同的表单元素
function setFormValue(name,value,formid){
	var FormObj=document.getElementById(formid)?document.getElementById(formid):null;
	if(FormObj==null)
	{
		Obj=document.getElementsByName(name);
		//alert(name+'-'+'1'+Obj);
	}
	else
	{
		Obj=eval('FormObj["'+name+'"]');
		//alert(name+'-'+'0'+Obj);
	}
	Obj=document.getElementsByName(name);
	if(Obj!=undefined){
//		try{
//			if(FormObj!=null){
//				type=Obj.type;
//				Obj=new Array(Obj);
//			}else if(Obj.length!=undefined){
//				type=Obj[0].type;
//			}
//		}catch(e){
//			type=Obj.type;
//			Obj=new Array(Obj);
//		}
		try{
			if(Obj.length==undefined){
				type=Obj.type;
				Obj=new Array(Obj);
			}
			else{
				if(Obj.type=='select-one')
				{
					type='select';
				}
				else
				{
				type=Obj[0].type;
				}
			}
		}
		catch(e){
			type=Obj.type;
			Obj=new Array(Obj);
		}
		//alert(name+'---'+type+'---'+Obj.length);
		if(type=="radio"){
			for(i=0;i<Obj.length;i++){
				if(Obj[i].value==value){
					Obj[i].checked=true;
				}
			}
		}
		else if(type=="text"&&Obj.length>1)
		{
			for(i=0;i<Obj.length;i++){
				if(value.split(',')[i]!=undefined){
				Obj[i].value=value.split(',')[i];
				}
			}
		}
		else if(type=="checkbox"){
			value=","+value+",";
			for(i=0;i<Obj.length;i++){
				if(value.indexOf(","+Obj[i].value+",")>-1){
						Obj[i].checked=true;
				}
			}
		}
		else if(type=="select")
		{
			Obj.value=value;
		}
		else if(type=="textarea"){
			var frameId="";
			for(i=0;i<document.getElementsByTagName("iframe").length;i++){
				if(document.getElementsByTagName("iframe")[i].src.indexOf(name)>=0){
					frameId=document.getElementsByTagName("iframe")[i].id;
				}
			}
			if(frameId==""){
				Obj[0].value=value;
			}else{
				eval(frameId+'.setHTML(value)');
			}
		}else{
			Obj[0].value=value;
		}
	}
	
}